$(function () {
//声明变量
    var shop_dialog =  $("#shop_dialog");
    var shop_form = $("#shop_form");
    var shop_datagrid =   $("#shop_datagrid");

    shop_datagrid.datagrid({
        url:'/shop/query.do',
        fit:true,
        fitColumns:true,
        toolbar:'#shop_toolbar',
        pagination:true,
        rownumber:true,
        singleSelect:true,
        columns:[[
            {field:'id',checkbox:true},
            {field:'name',title:'店铺名称',width:80},
            {field:'shopmanager',title:'店主姓名',width:80},
            {field:'industryType',title:'行业类型',width:80},
            {field:'phone',title:'店铺电话',width:80},
            {field:'address',title:'地址',width:80},
            {field:'productStock.id',title:'库存ID',width:80,formatter:function (value,row,index) {
                    return row?row.productStock.id:'';
                }},
            {field:'productStock.storeNumber',title:'库存数量',width:80,formatter:function (value,row,index) {
                    return row?row.productStock.storeNumber:'';
                }},
            {field:'depotId',title:'仓库ID',width:80},
            {field:'depotName',title:'仓库名称',width:80}

        ]]
    });
    //配置dialog
    shop_dialog.dialog({
        width:290,
        height:400,
        buttons:'#btn',
        close:true,
        onClose:function () {
            //关闭的同时清除表单数据
            shop_form.form('clear');
        }
    });

    var methodObj = {
        add:function(){
            shop_dialog.dialog('open');
            shop_dialog.dialog('setTitle','新增员工');
        },
        delete:function () {
            //获取一行数据
            var row = shop_datagrid.datagrid('getSelected');
            //判断是否有选择选择数据
            if (!row) {
                //没有选择数据提示用户
                $.messager.alert("温馨提示", "至少要选择一条数据", "warning");
                return;//终止操作
            }
            //弹出确认框
            $.messager.confirm('确认', '您确认想要执行操作吗？', function (r) {
                if (r) {
                    //给后台发送请求
                    $.get("/shop/deleteByPrimaryKey.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info', function () {
                                //加载刷新到当前界面
                                shop_datagrid.datagrid('reload');
                            })
                        } else {
                            $.messager.alert("温馨提示", "操作失败", "error");
                        }
                    })
                }
            })
        },
        edit:function (){
            //获取一行数据
            var row = shop_datagrid.datagrid('getSelected');
            $("#psw").hide();
            if (!row) {
                //没有选择数据提示用户
                $.messager.alert("温馨提示","至少要选择一条数据","warning");
                return;//终止操作
            }
            row['productStock.id'] = row.productStock.id;
            row['productStock.storeNumber'] = row.productStock.storeNumber;
            shop_form.form('load',row);

            //处理好回显数据后,打开编辑框
            shop_dialog.dialog('open');
            //设置标题
            shop_dialog.dialog('setTitle','编辑员工');
        },
        save:function (){
            //提交表单
            shop_form.form("submit",{
                url:'/shop/saveOrUpdate.do',

                success:function (data) {
                    //回调函数:data表示返回数据解析为json
                    data = $.parseJSON(data);
                    if (data.success) {
                        //如果返回结果成功__弹出窗口提示
                        $.messager.alert('温馨提示','保存成功','info',function () {
                            methodObj.cancel();
                            shop_datagrid.datagrid('reload');
                        })

                    }else {
                        //如果保存失败,则弹出窗口提示
                        $.messager.alert('温馨提示',data.msg,'error');
                    }
                }
            });
        },

        cancel: function (){
            //关闭弹出框:
            shop_dialog.dialog('close');

        },
        query:function (){
            //获取查询条件
            var keyword =  $("#keyword").textbox('getValue');
            //通过datagrid 的 load方法传递参数
            shop_datagrid.datagrid('load',{
                keyword:keyword
            });
        }

    };
    //统一绑定事件:
    $("a[data-cmd]").click(function () {
        var methodname = $(this).data("cmd");
        //动态获取调用方法名字,调用方法;
        methodObj[methodname]();
    })
});

//删除

//编辑

//============表单的 按钮 保存/取消=================


//保存密码


//高级查询


//修改密码

